
WATER_CMD = {
    '开高压': ['AA', '0000', '00', '01', '', '00'],
    '关高压': ['AA', '0000', '00', '02', '', '00'],
    '本底测量': ['AA', '0000', '00', '72', '', '00'],
    '开始测量': ['AA', '0000', '00', '71', '', '00'],
    '停止测量': ['AA', '0000', '00', '73', '', '00'],
    '单次测量': ['AA', '0000', '00', '74', '', '00'],
    '累积剂量测量': ['AA', '0000', '00', '75', '', '00'],
    '设置传输速度': ['AA', '0000', '00', '41', '', '00'],
    '设置走位速度': ['AA', '0000', '00', '07', '', '00'],
    '回程差x': ['AA', '0000', '00', '21', '', '00'],
    '回程差y': ['AA', '0000', '00', '22', '', '00'],
    '回程差z': ['AA', '0000', '00', '23', '', '00'],
    '回程差': ['AA', '0000', '00', '52', '', '00'],
    '电机走步x+': ['AA', '0000', '00', '32', '', '00'],
    '电机走步x-': ['AA', '0000', '00', '31', '', '00'],
    '电机走步y+': ['AA', '0000', '00', '34', '', '00'],
    '电机走步y-': ['AA', '0000', '00', '33', '', '00'],
    '电机走步z+': ['AA', '0000', '00', '36', '', '00'],
    '电机走步z-': ['AA', '0000', '00', '35', '', '00'],
    '电机走步': ['AA', '0000', '00', '51', '', '00'],
    '连续测量': ['AA', '0000', '00', '50', '', '00'],
    '数据写入': ['AA', '0000', '00', '42', '', '00'],
    '数据读取': ['AA', '0000', '00', '43', '', '00'],
    '复位坐标': ['AA', '0000', '00', '44', '', '00'],
    '读取坐标': ['AA', '0000', '00', '53', '', '00'],
    # '读取限位': ['AA', '0000', '00', '6A', '', '00'],
    '读取限位': ['AA', '0000', '00', '6B', '', '00'],
    '读取信息': ['AA', '0000', '00', '82', '', '00'],
    '设置限位': ['AA', '0000', '00', '6A', '', '00'],
    '移动停止': ['AA', '0000', '00', '54', '', '00'],
    '停止':     ['AA', '0000', '00', '55', '', '00'],
    '任务坐标': ['AA', '0000', '00', '5A', '', '00'],
    'x+': ['AA', '0000', '00', '45', '', '00'],
    'x-': ['AA', '0000', '00', '46', '', '00'],
    'y+': ['AA', '0000', '00', '47', '', '00'],
    'y-': ['AA', '0000', '00', '48', '', '00'],
    'z+': ['AA', '0000', '00', '49', '', '00'],
    'z-': ['AA', '0000', '00', '4A', '', '00'],
    'lowspeed': ['AA', '0000', '00', '4B', '', '00'],
    'highspeed': ['AA', '0000', '00', '4C', '', '00'],
}

class ComDeal():

    @staticmethod
    def getCmd(cmd_list):
        '''生成命令'''
        cmd = ''.join(cmd_list)
        cmd.upper()
        cmd_start = cmd_list[0]
        cmd_len = hex(int(len(cmd) / 2)).replace('0x', '').zfill(4).upper()
        cmd_list[1] = cmd_len
        if len(cmd_len) == 4:
            hex_len = int(cmd_len[:2], 16) + int(cmd_len[-2:], 16)
            cmd_sum = (hex(int(cmd_start, 16) + int(cmd_len[:2], 16) + int(cmd_len[-2:], 16))).upper()[-2:]
        if len(cmd_len) == 2:
            hex_len = int(cmd_len, 16)
            cmd_sum = (hex(int(cmd_start, 16) + int(cmd_len, 16))).upper()[-2:]
        cmd_list[2] = cmd_sum
        s = 0
        s = int(cmd_list[0], 16) + hex_len + int(cmd_list[2], 16) + int(cmd_list[3], 16)
        for i in range(0, len(cmd_list[4])):
            if i % 2 == 0:
                s = s + int(cmd_list[4][i:i + 2], 16)
        s = hex(s).upper()[-2:]
        cmd_list[-1] = s
        cmd = ''.join(cmd_list)
        return cmd

    @staticmethod
    def getSum(hex_str=""):
        """计算校验和,取校验和低八位"""
        if hex_str:
            s = 0
            for i in range(0, len(hex_str)):
                if i % 2 == 0:
                    s = s + int(hex_str[i:i + 2], 16)
            s = hex(s).replace('0x','').upper()
            if len(s) > 2:
                s = s[-2:]
            else:
                s = s.zfill(2)
            return s
        return False


# com = ComDeal()
# res = com.getSum(hex_str="0102")
# print(res)
